<template>
    <div class="asset-list">
		<nav-header></nav-header>

		<div class="zh-table">
            <div class="zh-table-open clear">
                <div class="zh-open-left fl">
                    <div class="open-width" style="width: 145px">
                        <el-select v-model="listParam.stateId" filterable placeholder="请选择状态">
                            <el-option
                                v-for="item in stateData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </div>
                    <div class="open-width" style="width: 145px">
                        <el-select v-model="listParam.classificationId" filterable placeholder="请选择分类">
                            <el-option
                                v-for="item in classificationData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </div>

                    <div class="open-width">
                        <el-input v-model="listParam.keyword" placeholder="资产名称、保管人、资产编号" @keyup.enter.native="searchFunc"></el-input>
                    </div>
                    <el-button type="primary" @click="searchFunc">搜索</el-button>
                    <el-button type="primary" @click="resetList">重置</el-button>
                </div>
                <div class="zh-open-right fr">
                    <el-button type="primary" @click="exportQrCode">导出二维码</el-button>
                    <el-button type="primary" @click="addAsset">添加资产</el-button>
                    <el-button type="primary" @click="batchImport">批量导入</el-button>
                    <el-button type="primary" @click="getAllData">导出Excel表</el-button>
                    <el-button type="danger" @click="dleteAllConfirm">批量删除</el-button>
                </div>
            </div>

            <div class="zh-table-contents" v-loading="loading.status" :element-loading-text="loading.txt">
                <el-table
                    :data="listData"
                    fixed
                    border
                    max-height="600"
                    @selection-change="tableSelectionChange"
                    style="width: 100%">
                    <el-table-column
                        type="selection"
                        width="50"
                        fixed
                        align="center">
                    </el-table-column>
                    <el-table-column
                        prop="identifier"
                        label="资产编号"
                        align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="name"
                        label="资产名称"
                        align="center"
                        width="140"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="buy_time"
                        label="购买时间"
                        align="center"
                        width="120"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="type_name"
                        label="资产分类"
                        align="center"
                        width="100"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="信息（品牌-型号）"
                        align="center"
                        width="130"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            {{scope.row.brand ? scope.row.brand : '-'}}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="number"
                        label="数量"
                        align="center"
                        width="70"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="place"
                        label="存放点"
                        align="center"
                        width="100"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="owner"
                        label="归属"
                        align="center"
                        width="120"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop="manager"
                        label="保管人"
                        align="center"
                        width="160"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="资产状态"
                        align="center"
                        width="160"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            <span v-for="(item, inx) in scope.row.spread" :key="inx">
                                <span style="color: #3BB080" v-if="item.status == 2"> 使用中({{item.number}}) </span>
                                <span style="color: #666" v-if="item.status == 1"> 未使用({{item.number}}) </span>
                                <span style="color: #E83D4E" v-if="item.status == 3"> 损坏({{item.number}})</span>
                                <span style="color: #E83D4E" v-if="item.status == 4"> 丢失({{item.number}})</span>
                                <span style="color: #E83D4E" v-if="item.status == 5"> 报废({{item.number}})</span>
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="固定资产图片"
                        align="center"
                        width="80"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            <span v-if="scope.row.thumb" class="check-row" @click="checkImg(scope.row)">查看</span>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="comment"
                        label="备注"
                        align="center"
                        width="80"
                        show-overflow-tooltip>
                        <template slot-scope="scope">
                            <span v-if="scope.row.comment" class="check-row" @click="checkRemark(scope.row)">查看</span>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        align="center"
                        :width="240">
                        <template slot-scope="scope">
                            <el-button @click="goToDetail(scope.row.id)" type="text">详情</el-button>
                            <el-button @click="goToEdit(scope.row.id)" type="text">编辑</el-button>
                            <el-button @click="exportOneQrCode(scope.row.id)" type="text">下载二维码</el-button>
                            <el-button @click="deleteConfirm(scope.row.id)" type="text" class="delete-button">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <div class="table-pagination" v-show="total_number > 10">
                    <el-pagination
                        @size-change="setCurrentSize"
                        @current-change="setCurrentPage"
                        :current-page="listParam.pageIndex"
                        :page-sizes="[10, 20, 30, 40, 50, 100]"
                        :page-size="listParam.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total_number">
                    </el-pagination>
                </div>
            </div>
        </div>

        <image-viewer
            v-if="imagePreview.showViewer"
            :initial-index="imageIndex"
            :on-close="closeViewer"
            :url-list="imagePreview.previewSrcList">
        </image-viewer>
    </div>
</template>

<script>
import XLSX from 'xlsx'
import FileSaver from 'file-saver'
import {str2Buffer} from './../../../tools/tool'

export default {
    data() {
        return {
            imagePreview: {
                showViewer: false,
                previewSrcList: []
            },
            imgVisible: false, // 图片预览
            imgViewUrl: null, // 图片地址
            rowLen: 0, // 删除数据的长度
            total_number: 0, // 资产列表总数
            selected: [], // 数据多选
            listData: [], // 资产列表数据
            stateData: [], // 状态列表
            classificationData: [], // 分类列表
            loading: { // 数据加载状态
                status: false,
                txt: '获取数据...'
            },
            listParam: { // 获取列表参数
                stateId: 0, // 状态 id
                classificationId: null, // 分类 id
                keyword: null, // 关键字
                pageIndex: 1,
                pageSize: 10
            }
        }
    },
    created() {
        this.getList();
        this.getClassification();

        this.stateData = [
            {
                id: 0,
                name: '全部状态'
            },
            {
                id: 1,
                name: '未使用'
            },
            {
                id: 2,
                name: '使用中'
            },
            {
                id: 3,
                name: '损坏'
            }
            ,{
                id: 4,
                name: '丢失'
            },
            {
                id: 5,
                name: '报废'
            }
        ]
    },
    computed: {
        imageIndex() {
            return this.imagePreview.previewSrcList.indexOf(this.imgViewUrl);
        }
    },
    methods: {
        /**
         * 查看图片
         * @param {Object} obj 记录信息
         */
        checkImg(obj) {
            this.imgViewUrl = this.BASE_URL + obj.thumb;
            this.imagePreview.showViewer = true;
            this.imagePreview.previewSrcList = [this.BASE_URL + obj.thumb];
        },
        closeViewer() {
            this.imagePreview.showViewer = false;
        },
        /**
         * 获取资产分类
         */
        getClassification() {
            let success = (res=>{
                this.classificationData = res.data;
            })

            this.$ajax.get('api/property/type', {func: {success: success}})
        },
        /**
         * 获取资产列表
         */
        getList() {
            this.loading.status = true;

            let success = (res=>{
                this.loading.status = false;
                this.listData = res.data.data;
                this.total_number = res.data.data_sum;
            })
            let param = {
                page_index: this.listParam.pageIndex,
                page_size: this.listParam.pageSize,
                type_id: this.listParam.classificationId,
                keyword: this.listParam.keyword,
                status: this.listParam.stateId
            }

            this.$ajax.get('api/property/lists',{params: param, func:{success: success}})
        },
        /**
         * 搜索列表
         */
        searchFunc() {
            this.listParam.pageIndex = 1;
            this.getList();
        },
        /**
         * 重置资产列表
         */
        resetList() {
            this.listParam = {
                stateId: 0,
                classificationId: null,
                keyword: null,
                pageIndex: 1,
                pageSize: 10
            }

            this.getList();
        },
         /**
         * 导出二维码
         * @param {Number} id 记录 id
         */
        exportOneQrCode(id) {
            let params = {id};

            this.export_excel('api/property/qrcode', params, '资产管理二维码', 'qr');
        },
        /**
         * 批量导出二维码
         */
        exportQrCode() {
            let _id = [];

            if (this.selected.length === 0) {
                this.$message.warning('请选中要导出的资产二维码');

                return false;
            }

            this.selected.forEach(item => {
                _id.push(item.id);
            })

            let params = {
                id: _id
            }

            this.export_excel('api/property/multiqrcode', params, '资产管理二维码', 'qr');
        },
        /**
         * 导出Excel表
         */
        getAllData() {
            if(this.listData.length === 0){
                this.$message.warning("导出的数据为空！")
                return false;
            }

            this.exportExcelGet('api/property/multiexcel', {}, '资产管理');
        },
        /**
         * 获取资产状态
         * @param {Object} obj 资产信息
         */
        getSpread(obj) {
            let str = '';

            obj.forEach(item => {
                if (item.status == 1) {
                    str += `使用中(${item.number}) `;
                } else if (item.status == 2) {
                    str += `未使用(${item.number}) `;
                } else {
                    str += `损坏(${item.number}) `;
                }
            })

            return str;
        },
        /**
         * 批量删除
         */
        dleteAllConfirm() {
            if(this.selected.length === 0) {
                this.$message.warning("请选择要删除的项");
                return false;
            }

            let yes = (res => {
                let ids = [];
                this.rowLen = this.selected.length;

                this.selected.forEach((item, inx) => {
                    ids.push(item.id);
                })

                this.deleteList(ids);
            })
            let cancel = (res => {})

            this.e_confirm({yes: yes, cancel: cancel, txt: '确定要删除这些资产项吗？删除将不可恢复'});
        },
        /**
         * 删除记录
         * @param {Number} id 记录 id
         */
        deleteConfirm(id) {
            let yes = (res => {
                this.rowLen = 1;

                this.deleteList(id + '');
            })
            let cancel = (res => {})

            this.e_confirm({yes: yes, cancel: cancel, txt: '确定要删除该资产吗？删除将不可恢复'});
        },
        /**
         * 删除数据
         * @param {String} id 删除的 id
         */
        deleteList(id) {
            this.loading.status = true;

            let success = (res => {
                this.$message.success(res.msg);

                if (this.listData.length - this.rowLen === 0) {
                    if (this.listParam.pageIndex > 1) {
                        this.listParam.pageIndex-- ;
                    } else {
                        this.listParam.pageIndex = 1;
                    }
                }

                this.getList();
            });
            let fail = (res =>{
                this.$message.error(res.msg);

                this.getList();
            })

            this.$ajax.delete('api/property/single', {params: {id}, func: {success: success, fail: fail}})
        },
        /**
         * 查看备注
         * @param {Object} obj 记录信息
         */
        checkRemark(obj) {
            this.$alert(obj.comment, '备注', {
                confirmButtonText: '确定'
            });
        },
        /**
         * 添加资产
         */
        addAsset() {
            this.$router.push("/affairs/asset/asset-add");
        },
        /**
         * 批量导入
         */
        batchImport() {
            this.$router.push("/affairs/asset/asset-batch-import");
        },
        /**
         * 跳转到详情页面
         * @param {Number} id 记录id
         */
        goToDetail(id) {
            this.$router.push('/affairs/asset/asset-detail/' + id);
        },
        /**
         * 跳转到编辑页面
         * @param {Number} id 记录id
         */
        goToEdit(id) {
            this.$router.push('/affairs/asset/asset-edit/' + id);
        },
        /**
         * 列表数据多选
         * @param {Array} val 选中数据的数组
         */
        tableSelectionChange(val) {
            this.selected = val;
        },
        /**
         * 设置每页列表的数量
         * @param {Number} val 每页数据条数
         */
        setCurrentSize(val) {
            this.listParam.pageIndex = 1;
            this.listParam.pageSize = val;

            this.getList();
        },
        /**
         * 设置当前列表的页数
         * @param {Number} val 页数
         */
        setCurrentPage(val) {
            this.listParam.pageIndex = val;

            this.getList();
        },
    },
}
</script>

<style lang="scss">
.asset-list {
    .check-row {
        color: #348EFC;
        cursor: pointer;
    }
}
</style>
